package com.demo2do.fms.admin.entity.search.business;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 * 查询安吉调度指令，生成派车单 的search
 * 
 * @author Chang Liu
 */
public class ShipmentSearch implements QueryBuilder{
    
    /** 所选择公司 */
    private Long organizationId;
    
    /** 调度指令号 */
    private String shipmentNumber;
    
    /** 驳运车号 */
    private Long transMeansId;
    
    /** 托运客户 */
    private Long customerId;
    
    /** 制造商*/
    private Long manufactorId;
    
    /** 起始地 */
    private Long srcWhId;
    
    /** 目的地 装卸地 */
    private Long destWhId;
    
    /** 运输方式*/
    private String transType;
    
    /** 日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginCreateDate;
    
    /** 日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endCreateDate;
    
    /** 开始装运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginLoadingDate;
    
    /** 结束装运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endLoadingDate;
    
    /** 开始起运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginLeavingDate;
    
    /** 结束起运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endLeavingDate;
    
    /** 调度指令状态 */
    private Long status;
    
    private String target;
        
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;

    /**
     * the default constructor
     */
    public ShipmentSearch() {
        queryOrder = new QueryOrder().add("createTime", "DESC");
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(queryFilter == null || refresh) {

            queryFilter = new QueryFilter().addEquals("transCorpId", organizationId)
                                           .addEquals("shipmentNumber", shipmentNumber)
                                           .addEquals("transMeansId", transMeansId)
                                           .addEquals("customerId", customerId)
                                           .addEquals("manufactorId", manufactorId)
                                           .addEquals("srcWhId",srcWhId)
                                           .addEquals("destWhId", destWhId)
                                           .addEquals("transType", transType)
                                           .addDateBetween("createTime", beginCreateDate, endCreateDate)
                                           .addDateTimeBetween("loadingTime", beginLoadingDate, endLoadingDate)
                                           .addDateTimeBetween("leavingTime", beginLeavingDate, endLeavingDate)
                                           .addEquals("status", status);
        }
        
        // 生成派车单查询时，需要加上派车单为空的条件
        if (StringUtils.equals("generate", target) || StringUtils.equals("generate-update", target)) {
            queryFilter.addIsNull("dispatchOrderId");
        }
        
        return queryFilter;
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM ShipmentLine WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + queryOrder.sentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }
    
    /**
     * @return the organizationId
     */
    public Long getOrganizationId() {
        return organizationId;
    }

    /**
     * @return the shipmentNumber
     */
    public String getShipmentNumber() {
        return shipmentNumber;
    }

    /**
     * @return the transMeansId
     */
    public Long getTransMeansId() {
        return transMeansId;
    }

    /**
     * @return the customerId
     */
    public Long getCustomerId() {
        return customerId;
    }

    /**
     * @return the manufactorId
     */
    public Long getManufactorId() {
        return manufactorId;
    }

    /**
     * @return the srcWhId
     */
    public Long getSrcWhId() {
        return srcWhId;
    }

    /**
     * @return the destWhId
     */
    public Long getDestWhId() {
        return destWhId;
    }

    /**
     * @return
     */
    public String getTransType() {
		return transType;
	}

    /**
     * @return the beginCreateDate
     */
    public Date getBeginCreateDate() {
        return beginCreateDate;
    }

    /**
     * @return the endCreateDate
     */
    public Date getEndCreateDate() {
        return endCreateDate;
    }

    /**
     * @return the beginLoadingDate
     */
    public Date getBeginLoadingDate() {
        return beginLoadingDate;
    }

    /**
     * @return the endLoadingDate
     */
    public Date getEndLoadingDate() {
        return endLoadingDate;
    }

    /**
     * @return the beginLeavingDate
     */
    public Date getBeginLeavingDate() {
        return beginLeavingDate;
    }

    /**
     * @return the endLeavingDate
     */
    public Date getEndLeavingDate() {
        return endLeavingDate;
    }

    /**
     * @return the status
     */
    public Long getStatus() {
        return status;
    }

    /**
     * @return the target
     */
    public String getTarget() {
        return target;
    }

    /**
     * @param organizationId the organizationId to set
     */
    public void setOrganizationId(Long organizationId) {
        this.organizationId = organizationId;
    }

    /**
     * @param shipmentNumber the shipmentNumber to set
     */
    public void setShipmentNumber(String shipmentNumber) {
        this.shipmentNumber = StringUtils.trim(shipmentNumber);
    }

    /**
     * @param transMeansId the transMeansId to set
     */
    public void setTransMeansId(Long transMeansId) {
        this.transMeansId = transMeansId;
    }

    /**
     * @param customerId the customerId to set
     */
    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    /**
     * @param manufactorId the manufactorId to set
     */
    public void setManufactorId(Long manufactorId) {
        this.manufactorId = manufactorId;
    }

    /**
     * @param srcWhId the srcWhId to set
     */
    public void setSrcWhId(Long srcWhId) {
        this.srcWhId = srcWhId;
    }

    /**
     * @param destWhId the destWhId to set
     */
    public void setDestWhId(Long destWhId) {
        this.destWhId = destWhId;
    }

    /**
     * @param transType
     */
    public void setTransType(String transType) {
		this.transType = transType;
	}

    /**
     * @param beginCreateDate the beginCreateDate to set
     */
    public void setBeginCreateDate(Date beginCreateDate) {
        this.beginCreateDate = beginCreateDate;
    }

    /**
     * @param endCreateDate the endCreateDate to set
     */
    public void setEndCreateDate(Date endCreateDate) {
        this.endCreateDate = endCreateDate;
    }

    /**
     * @param beginLoadingDate the beginLoadingDate to set
     */
    public void setBeginLoadingDate(Date beginLoadingDate) {
        this.beginLoadingDate = beginLoadingDate;
    }

    /**
     * @param endLoadingDate the endLoadingDate to set
     */
    public void setEndLoadingDate(Date endLoadingDate) {
        this.endLoadingDate = endLoadingDate;
    }

    /**
     * @param beginLeavingDate the beginLeavingDate to set
     */
    public void setBeginLeavingDate(Date beginLeavingDate) {
        this.beginLeavingDate = beginLeavingDate;
    }

    /**
     * @param endLeavingDate the endLeavingDate to set
     */
    public void setEndLeavingDate(Date endLeavingDate) {
        this.endLeavingDate = endLeavingDate;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(Long status) {
        this.status = status;
    }

    /**
     * @param target the target to set
     */
    public void setTarget(String target) {
        this.target = target;
    }
}
